import net.sourceforge.tess4j.Tesseract1;
import net.sourceforge.tess4j.TesseractException;
import org.apache.commons.httpclient.util.HttpURLConnection;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;

/**
 * Created by ChoviWu on 2018/05/06
 * Description:
 */
public class GetImg {

    private static final String fileName = "captcha.jpg";

    //根据实际验证码的色彩来判断哪里要变成白色
    public static int isWhite(int colorInt) {
        Color color = new Color(colorInt);
        if (color.getRed() + color.getGreen() + color.getBlue() > 300) {  //根据实际情况修改这里的300
            return 1;
        }
        return 0;
    }

    //根据实际验证码的色彩来判断哪里要变成黑色
    public static int isBlack(int colorInt) {
        Color color = new Color(colorInt);
        if (color.getRed() + color.getGreen() + color.getBlue() <= 300) {  //根据实际情况修改这里的300
            return 1;
        }
        return 0;
    }

    //扫描验证码所有的像素颜色过滤掉不要的颜色
    public static BufferedImage removeBackgroud4Tone(String picFile)
            throws Exception {
        BufferedImage img = ImageIO.read(new File(picFile));
        int width = img.getWidth();
        int height = img.getHeight();
        for (int x = 0; x < width; ++x) {
            for (int y = 0; y < height; ++y) {
                if (isWhite(img.getRGB(x, y)) == 1) {
                    img.setRGB(x, y, Color.WHITE.getRGB());
                } else {
                    img.setRGB(x, y, Color.BLACK.getRGB());
                }
            }
        }
        return img;
    }

    //将过滤都的黑白图片保存
    public static void handleImg(String file) {
        BufferedImage img;
        try {
            img = removeBackgroud4Tone(file);

            ImageIO.write(img, "JPG", new File(fileName));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    /*public static void handleImg(String file, int count) {
        BufferedImage img;
        try {
            img = removeBackgroud4MaxColor(file, count);

            ImageIO.write(img, "JPG", new File(fileName));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }*/

    //识别验证码，这里如果验证码处理不好识别率很低
    public static String identifyCode() {
        handleImg(fileName);
        Tesseract1 instance = new Tesseract1();
        File imageFile = new File(fileName); // instance.setLanguage("chi_sim");
        String result = null;
        try {
            result = instance.doOCR(imageFile);
        } catch (TesseractException e) { // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return result;
    }

    //获得网站的验证码及COOKIE
    public static HashMap<String, String> getCode(String uri) {
        HashMap<String, String> map = new HashMap<String, String>();
        try {
            URL url = new URL(uri);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("GET"); // 以Post方式提交表单，默认get方式
            String cookie = con.getHeaderField("set-cookie");
            ImageIO.write(ImageIO.read(con.getInputStream()), "JPG", new File(
                    fileName));
            String code = identifyCode();
            map.put("cookie", cookie); //cookie=JSESSIONID=16yjdmlj4l1g81jqe39c41nooc;
            map.put("code", code);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return map;
    }
}
